/*
 * QTeXPreview
 * Copyright (C) 2008, Philip Pronin (philip.pronin@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "toolBoxConfig.h"
#include "toolBoxTabConfig.h"

#include <QDataWidgetMapper>
#include <QInputDialog>
#include <QStandardItemModel>

ToolBoxConfig::ToolBoxConfig(QWidget* parent) : QDialog(parent) {
	setupUi(this);

	mapper = new QDataWidgetMapper();
	mapper->setModel(listViewEx->getModel());
	mapper->addMapping(lineEditTitle, 0);

	connect(listViewEx->getSelectionModel(),
		SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
		SLOT(selectionChanged(const QItemSelection &)));
	connect(listViewEx, SIGNAL(beforeRowDelete(const QList<QStandardItem*>&)),
		SLOT(beforeRowDelete(const QList<QStandardItem*>&)));
}

void ToolBoxConfig::addItem() {
	addGroup(ToolBoxGroup(tr("[new]")));

	QStandardItemModel* model = listViewEx->getModel();
	listViewEx->getSelectionModel()->setCurrentIndex(model->index(model->rowCount() - 1, 0),
		QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}

void ToolBoxConfig::selectionChanged(const QItemSelection& selected) {
	QItemSelectionModel* selModel = listViewEx->getSelectionModel();
	groupBox->setEnabled(selModel->currentIndex().isValid());
	mapper->setCurrentIndex(selModel->currentIndex().row());
}

void ToolBoxConfig::editCommands() {
	QModelIndexList indexes = listViewEx->getSelectionModel()->selectedRows();
	if (indexes.size() < 1) return;
	QStandardItemModel* model = listViewEx->getModel();
	QStandardItem* key = model->item(indexes[0].row());

	ToolBoxTabConfig tbtc(this);
	tbtc.setWindowTitle(tbtc.windowTitle() + " - " + key->text());
	tbtc.setCommandList(cmds[key]);
	if (tbtc.exec() == QDialog::Accepted)
		cmds[key] = tbtc.getCommandList();
}

void ToolBoxConfig::addGroup(const ToolBoxGroup& group) {
	QStandardItem* headItem = new QStandardItem(group.title);
	headItem->setCheckable(true);
	headItem->setCheckState(group.active ? Qt::Checked : Qt::Unchecked);
	cmds[headItem] = group.commands;
	listViewEx->getModel()->appendRow(headItem);
}

void ToolBoxConfig::beforeRowDelete(const QList<QStandardItem*>& list) {
	cmds.remove(list[0]);
}

ToolBoxGroupList ToolBoxConfig::getGroupList() const {
	ToolBoxGroupList list;

	QStandardItemModel* model = listViewEx->getModel();
	for (int i = 0; i < model->rowCount(); i++) {
		QStandardItem* headItem = model->item(i, 0);
		ToolBoxGroup group(headItem->text(), headItem->checkState() == Qt::Checked);
		group.commands = cmds[headItem];
		list << group;
	}

	return list;
}

void ToolBoxConfig::setGroupList(ToolBoxGroupList list) {
	QStandardItemModel* model = listViewEx->getModel();
	model->clear(); cmds.clear();
	foreach(ToolBoxGroup group, list) addGroup(group);
	if (model->rowCount() > 0)
		listViewEx->getSelectionModel()->setCurrentIndex(model->index(0, 0),
			QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
